package com.aeon.jface.tableViewer.ui;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;

public class MFilter extends ViewerFilter {

	private String mRegex = "*";

	@Override
	public boolean select(Viewer viewer, Object parentElement, Object element) {
		if (element instanceof SymbolDescriptor) {
			SymbolDescriptor descriptor = (SymbolDescriptor) element;
			try {
				if (descriptor.getImageName().matches(getRegex())) {
					return true;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return false;
	}

	/**
	 * @return the mRegex
	 */
	private String getRegex() {
		StringBuilder or = new StringBuilder(mRegex);
		int nexti = 0;
		while (nexti<or.length()&&((nexti = or.indexOf("*", nexti)) != -1)) {
			or.insert(nexti, ".");
			nexti+=2;
		}
		return or.toString();
	}

	/**
	 * @param mRegex
	 *            the mRegex to set
	 */
	public void setRegex(String mRegex) {
		this.mRegex = mRegex;
	}

}
